package com.hyzs.utils;

import org.neo4j.driver.v1.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
import org.springframework.beans.factory.annotation.Value;

@Component
@ConfigurationProperties(prefix ="graph")
public class Neo4jConnectionUtils {

    //private static String conn_url = "bolt://172.16.119.31:7687";
//    private static String conn_url = "bolt://graph-db:7687";

    private String url;

    private String user;

    private String password;

    private Config config = Config.builder()
            .withMaxConnectionLifetime( 10, TimeUnit.MINUTES )
            .withMaxConnectionPoolSize( 5 )
            .withConnectionAcquisitionTimeout( 2, TimeUnit.MINUTES )
            .build();
    private Driver driver ;

    public Driver getDriver() {
        if(driver == null)
            driver = GraphDatabase.driver(url, AuthTokens.basic(user, password), config);
        return driver;
    }

     public Session getSession() {
        if(driver == null)
            return getDriver().session();
        return driver.session();
    }


    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}
